package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author mgx
 * @version 1.0
 * @description: TODO
 * @date 2022/10/27 7:59
 */
@RestController
@RequestMapping("/admin/product")
public class SkuManageController {

    @Autowired
    private ManagerService managerService;

    /**
     * 根据spuId获取相关的销售属性和销售属性值
     * /admin/product/spuSaleAttrList/{spuId}
     * @param spuId
     * @return
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId) {
        List<SpuSaleAttr> spuSaleAttrList = managerService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }


    /**
     * 根据spuId查询图片列表
     * /admin/product/spuImageList/{spuId}
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId) {
        List<SpuImage> spuImageList = managerService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }


    /**
     * 保存sku
     * /admin/product/saveSkuInfo
     * @param skuInfo
     * @return
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        managerService.saveSkuInfo(skuInfo);
        return Result.ok();
    }


    /**
     * 分页查询skuInfo
     * /admin/product/list/{page}/{limit}
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuInfoPage(@PathVariable Long page,
                                 @PathVariable Long limit) {
        Page<SkuInfo> infoPage = new Page<SkuInfo>(page, limit);
        IPage<SkuInfo> skuInfoIPage = managerService.getSkuInfoPage(infoPage);
        return Result.ok(skuInfoIPage);
    }


    /**
     * 商品上架
     * /admin/product/onSale/{skuId}
     * @param skuId
     * @return
     */
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId) {
        managerService.onSale(skuId);
        return Result.ok();
    }

    /**
     * 商品下架
     * /admin/product/cancelSale/{skuId}
     * @param skuId
     * @return
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId) {
        managerService.cancelSale(skuId);
        return Result.ok();
    }
}
